package com.kerbores.utils.listener;

import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;

import org.apache.log4j.PropertyConfigurator;
import org.nutz.ioc.impl.PropertiesProxy;
import org.nutz.lang.Strings;
import org.nutz.log.Log;
import org.nutz.log.Logs;

/**
 * @author 王贵源
 * @email kerbores@kerbores.com
 *
 *        create at 2015年10月14日 下午6:55:21
 */
public class KerboresLogLoadListener implements ServletContextListener {

	Log log = Logs.get();

	/*
	 * (non-Javadoc)
	 * 
	 * @see javax.servlet.ServletContextListener#contextDestroyed(javax.servlet.
	 * ServletContextEvent)
	 */
	@Override
	public void contextDestroyed(ServletContextEvent arg0) {
		// TODO Auto-generated method stub

	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * javax.servlet.ServletContextListener#contextInitialized(javax.servlet
	 * .ServletContextEvent)
	 */
	@Override
	public void contextInitialized(ServletContextEvent arg0) {
		log.info("loading log4j...");
		PropertiesProxy proxy = null;
		String env = System.getenv("env_type");// 获取环境
		log.info("env-type:---> " + env);
		if (!Strings.isBlank(env)) {// 如果是线上环境 尝试加载线上日志
			proxy = new PropertiesProxy(false, "log4j-online.properties");
			PropertyConfigurator.configure(proxy.toProperties());
		}
	}

}
